package com.contract.common.util;

import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 日期工具
 *
 * @author liw
 * @version 1.0
 * @date 2022/6/10 16:39
 */
@Slf4j
@UtilityClass
public class DateUtil {
    //格式化日期

    // 列"20180602"  length = 8
    public static final String DATE_YMD = "yyyyMMdd";
    private static final String FORMAT_YMD = "[0-9]{8}";

    // 列"2018-06-02 21:03:23"  length = 19
    public static final String DATE_YMDHMS_ = "yyyy-MM-dd HH:mm:ss";
    private static final String FORMAT_YMDHMS_ = "[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}";

    /**
     * 获取SimpleDateFormat
     *
     * @param dateFormat 需要格式化的格式
     * @return SimpleDateFormat
     */
    public static DateTimeFormatter getDtf(String dateFormat) {

        return DateTimeFormatter.ofPattern(dateFormat);

    }

    /**
     * 日期-> 字符串
     *
     * @param date       日期
     * @param dateFormat 格式
     * @return 字符串
     */
    public static String date2Str(LocalDateTime date, String dateFormat) {
        String str = null;
        if (!ObjectUtils.isEmpty(date)) {
            DateTimeFormatter sdf = getDtf(dateFormat);
            str = sdf.format(date);
        }
        return str;
    }


}
